// 默认缓存期限为7天
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
export default class Storage {
    private storage: globalThis.Storage
    private prefixKey: string
    constructor(prefixKey: string, storage = localStorage) {
        this.storage = storage,
            this.prefixKey = prefixKey
    }
    private getKey(key: string) {
        return `${this.prefixKey}${key}`.toUpperCase()
    }
    set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
        const stringData = JSON.stringify({
            value,
            expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
        });
        this.storage.setItem(this.getKey(key), stringData);
    }
    get(key: string) {
        const item = this.storage.getItem(this.getKey(key))
        if (!item) return null
        const { value, expire } = JSON.parse(item)
        if (expire > Date.now()) {
            this.remove(key)
            return null
        }
        else {
            return value
        }

    }
    remove(key: string): void {
        this.storage.removeItem(this.getKey(key))
    }
    clear(): void {
        this.storage.clear()
    }
    setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
        document.cookie = `${this.getKey(name)}=${value}; Max-Age=${expire}`;
    }

    getCookie(name: string): string {
        const cookieArr = document.cookie.split('; ');
        for (let i = 0, length = cookieArr.length; i < length; i++) {
            const kv = cookieArr[i].split('=');
            if (kv[0] === this.getKey(name)) {
                return kv[1];
            }
        }
        return '';
    }

    removeCookie(key: string) {
        this.setCookie(key, 1, -1);
    }

    clearCookie(): void {
        const keys = document.cookie.match(/[^ =;]+(?==)/g);
        if (keys) {
            for (let i = keys.length; i--;) {
                document.cookie = keys[i] + '=0;expire=' + new Date(0).toUTCString();
            }
        }
    }
}